package cn.est.controller;
import cn.est.pojo.Model;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.ModelService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/model")
public class ModelController {

    @Resource
    private ModelService modelService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Model> searchModel(@RequestParam Map<String,Object> param) {
        Page<Model> modelPage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            modelPage  = modelService.queryModelPageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelPage;
    }

    @RequestMapping("/saveOrUpdateModel")
    @ResponseBody
    public Result saveOrUpdateModel(Model model){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(model.getId())){
                flag=modelService.qdtxAddModel(model);
            }else{
                flag=modelService.qdtxModifyModel(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getModelById")
    @ResponseBody
    public Result getModelById(Long id){
        Result result= ResultUtils.returnSuccess();
        Model model=null;
        try {
            model=modelService.getModelById(id);
            result.setData(model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveModel")
    @ResponseBody
    public Result batchRemoveModel(String ids){
        Integer flag=0;
        try {
            flag=modelService.qdtxBatchDeleteModel(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeModelById")
    @ResponseBody
    public Result removeModelById(Long id){
        Integer flag=0;
        try {
            flag=modelService.qdtxDeleteModelById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
